﻿#include "CSem.h"
#include <string.h>

#include<windows.h>

static int signal(struct _csem_* sem) {
	ReleaseSemaphore(sem->handle, 1, NULL);
	return 0;
}

static int wait(struct _csem_* sem) {
	WaitForSingleObject(sem->handle, INFINITE);
	return 0;
}

int construct_sem(CSem* sem, long initial_count) {
	memset(sem, 0, sizeof(*sem));

	sem->wait = wait;
	sem->signal = signal;

	sem->handle = CreateSemaphore(NULL, initial_count, LONG_MAX, NULL);
	if (0 == sem->handle) {
		//create failed
		return -1;
	}
	return 0;
}